<?php

namespace Frieza\Modules\Creators\Controllers;

use App\Http\Controllers\Controller;
use App\Pockets\GridDataModel as pGridDataModel;
use Doraemon\ali\model\Article\Article as mArticle;
use Doraemon\model\Blank as mBlank;
use Doraemon\model\CreatorsCollage;
use Doraemon\model\Photos as mPhotos;
use Doraemon\service\Creators\CreatorsCollage as sCreatorsCollage;
use Doraemon\tools\BlockType;
use Doraemon\tools\IdParse;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Tab;
use Illuminate\Http\Request;

class CollageController extends Controller
{

    public function collageList(Request $request, Content $content)
    {
        $pageSize = $request->input("per_page", 50);
        $page = $request->input("page", 1);
        $infos = sCreatorsCollage::getList($page, $pageSize);
        if (!empty($infos)) {
            //拼装格外数据
            $titles = mPhotos::select(['id' => ['title']], ['id' => array_keys($infos)]);
            foreach ($infos as $key => $value) {
                $infos[$key]['title'] = $titles[$key]['title'] ?: '无标题';
                $infos[$key]['tab_name'] = CreatorsCollage::TAB_MAP[$infos[$key]['tab']];
            }
        }
        $gridModel = new pGridDataModel($infos, CreatorsCollage::getCount(), ['perPage' => $pageSize, 'keyName' => 'obj_id']);
        $gridList = Admin::grid($gridModel,
            function (Grid $grid) use ($gridModel) {
                $grid->paginate($gridModel->getPerPage());
                $grid->disableExport();
                $grid->disableRowSelector();
                $grid->disableCreateButton();
                $grid->disableFilter();
                $grid->quickCreate(function (Grid\Tools\QuickCreate $create) {
                    $create->text('obj_id', 'obj_id');
                    $create->select('tab', '一级tab')->options(CreatorsCollage::TAB_MAP);
                    $create->select('tab_type', '二级tab')->options(CreatorsCollage::TYPE_MAP);
                    $create->text('weight', '权重');
                });
                $grid->column("obj_id", 'obj_id');
                $grid->column("title", '标题');
                $grid->column("obj_type", '类型')->display(function () {
                    return BlockType::name($this->obj_type);
                });
                $grid->column('tab_name', '一级tab');
                $grid->column('tab_type', '二级tab')->display(function () {
                    if ($this->tab == CreatorsCollage::TAB_CONTENT) {
                        return CreatorsCollage::TYPE_MAP[$this->tab_type];
                    }else{
                        return '图文';
                    }
                });
                $grid->column('weight', '权重')->editable();
                $grid->column("addtime", '创建时间')->display(function () {
                    return date("Y-m-d H:i:s", $this->addtime);
                });
                $grid->column("edittime", '更新时间')->display(function () {
                    return date("Y-m-d H:i:s", $this->edittime);
                });
                $grid->column("operator_id", '操作者')->display(function () {
                    return Admin::user()->find($this->operator_id)->name;
                });
                $grid->actions(function ($action) {
                    $action->disableEdit();
                });
            });
        $tab = new Tab();
        $tab->add('创作者学院', $gridList->render(), true);
        $content->row($tab);
        return $content
            ->header('创作者学院')
            ->description(' ')
            ->breadcrumb(['text' => '创作者学院']);
    }

    public function collageSubmit(Request $request, $id = 0)
    {
        try {
            $params = $request->all();
            $id ? sCreatorsCollage::updateObj($id, $params) : sCreatorsCollage::insterObj($params);
            return response()->json([
                'status' => true,
                'message' => '操作成功'
            ]);
        } catch (\Exception $exception) {
            return response()->json([
                'status' => false,
                'message' => $exception->getMessage()
            ], 400);
        }
    }

    public function collageDelete($id)
    {
        $result = sCreatorsCollage::deleteObj($id);
        if ($result) {
            return response()->json([
                'status' => true,
                'message' => '操作成功'
            ]);
        }
        return response()->json([
            'status' => false,
            'message' => '操作失败'
        ], 400);
    }

}